using Microsoft.ReportingServices.Diagnostics;
using Microsoft.ReportingServices.ReportIntermediateFormat.Persistence;
using Microsoft.ReportingServices.ReportProcessing.Persistence;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Microsoft.ReportingServices.ReportProcessing
{
	[Serializable]
	internal sealed class ValidValue : IPersistable
	{
		private object m_value;

		private string m_label;

		[NonSerialized]
		private string m_stringValue;

		[NonSerialized]
		private string m_cachedAutogenLabel;

		[NonSerialized]
		private bool m_labelAutoGenerated;

		[NonSerialized]
		private static readonly Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.Declaration m_Declaration = GetNewDeclaration();

		public object Value
		{
			get
			{
				return m_value;
			}
			set
			{
				m_value = value;
				m_cachedAutogenLabel = null;
			}
		}

		public string Label
		{
			get
			{
				if (m_label == null)
				{
					if (!m_labelAutoGenerated)
					{
						m_labelAutoGenerated = true;
						if (m_value != null)
						{
							m_cachedAutogenLabel = ParameterInfo.CastValueToLabelString(m_value, Localization.ClientPrimaryCulture);
						}
					}
					return m_cachedAutogenLabel;
				}
				return m_label;
			}
			set
			{
				m_label = value;
				m_cachedAutogenLabel = null;
				m_labelAutoGenerated = false;
			}
		}

		public string LabelRaw
		{
			get
			{
				return m_label;
			}
			set
			{
				Label = value;
			}
		}

		public string StringValue => m_stringValue;

		public ValidValue()
		{
		}

		public ValidValue(string validValue, string label)
		{
			m_stringValue = validValue;
			m_label = label;
		}

		public ValidValue(object validValue, string label)
		{
			m_value = validValue;
			m_label = label;
		}

		internal void EnsureLabelIsGenerated()
		{
			if (m_label == null && m_cachedAutogenLabel == null && m_value != null)
			{
				m_cachedAutogenLabel = ParameterInfo.CastValueToLabelString(m_value, Thread.CurrentThread.CurrentCulture);
			}
			m_labelAutoGenerated = true;
		}

		internal static Microsoft.ReportingServices.ReportProcessing.Persistence.Declaration GetDeclaration()
		{
			MemberInfoList memberInfoList = new MemberInfoList();
			memberInfoList.Add(new Microsoft.ReportingServices.ReportProcessing.Persistence.MemberInfo(Microsoft.ReportingServices.ReportProcessing.Persistence.MemberName.Label, Microsoft.ReportingServices.ReportProcessing.Persistence.Token.String));
			memberInfoList.Add(new Microsoft.ReportingServices.ReportProcessing.Persistence.MemberInfo(Microsoft.ReportingServices.ReportProcessing.Persistence.MemberName.Value, Microsoft.ReportingServices.ReportProcessing.Persistence.Token.Object));
			return new Microsoft.ReportingServices.ReportProcessing.Persistence.Declaration(Microsoft.ReportingServices.ReportProcessing.Persistence.ObjectType.None, memberInfoList);
		}

		internal static Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.Declaration GetNewDeclaration()
		{
			List<Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberInfo> list = new List<Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberInfo>();
			list.Add(new Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberInfo(Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Label, Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.Token.String));
			list.Add(new Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberInfo(Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Value, Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.Token.Object));
			return new Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.Declaration(Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.ObjectType.ValidValue, Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.ObjectType.None, list);
		}

		void IPersistable.Serialize(Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.IntermediateFormatWriter writer)
		{
			writer.RegisterDeclaration(m_Declaration);
			while (writer.NextMember())
			{
				switch (writer.CurrentMember.MemberName)
				{
				case Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Label:
					writer.Write(m_label);
					break;
				case Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Value:
					writer.Write(m_value);
					break;
				default:
					Global.Tracer.Assert(condition: false);
					break;
				}
			}
		}

		void IPersistable.Deserialize(Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.IntermediateFormatReader reader)
		{
			reader.RegisterDeclaration(m_Declaration);
			while (reader.NextMember())
			{
				switch (reader.CurrentMember.MemberName)
				{
				case Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Label:
					m_label = reader.ReadString();
					break;
				case Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.MemberName.Value:
					m_value = reader.ReadVariant();
					break;
				default:
					Global.Tracer.Assert(condition: false);
					break;
				}
			}
		}

		void IPersistable.ResolveReferences(Dictionary<Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.ObjectType, List<MemberReference>> memberReferencesCollection, Dictionary<int, IReferenceable> referenceableItems)
		{
			Global.Tracer.Assert(condition: false);
		}

		Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.ObjectType IPersistable.GetObjectType()
		{
			return Microsoft.ReportingServices.ReportIntermediateFormat.Persistence.ObjectType.ValidValue;
		}
	}
}
